/*
 * ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Topless JSF.
 *
 * The Initial Developer of the Original Code is
 * Marco Antonio Villalobos, Jr. (mvillalobos@kineteque.com).
 * Portions created by the Initial Developer are Copyright (C) 2010
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Marco Antonio Villalobos, Jr. (mvillalobos@kineteque.com).
 *
 * ***** END LICENSE BLOCK *****
 */

package kineteque.jsf.topless.util;

/**
 * A case converter for creating variable names.
 */
public class CaseConverter {

    /**
     * Converts camel case into human readable format.
     * @param c the camel case string to convert.
     * @return A human readable format.
     */
    public static String camelToHuman(String c) {
        StringBuilder v = new StringBuilder();
        char a[] = c.toCharArray();
        boolean first = true;
        for (char i:a) {
            if ('A' <= i && i <= 'Z') {
                v.append(' ').append(i);
            } else if (i=='_') {
                v.append(' ');
            } else if (first && Character.isLowerCase(i)) {
                v.append(Character.toUpperCase(i));
            } else {
                v.append(i);
            }
            first = false;
        }
        return v.toString();
    }

    /**
     * Converts a table name into human readable format.
     * @param t The table to convert.
     * @return A human readable format.
     */
    public static String tableToHuman(String t) {
        StringBuilder v = new StringBuilder();
        String s[] = t.split("\\_+");
        for (String i:s) {
            if (i.length()==1) {
                v.append(Character.toUpperCase(i.charAt(0))).append(' ');
            } else {
                v.append(Character.toUpperCase(i.charAt(0))).append(i.substring(1).toLowerCase()).append(' ');
            }
        }
        return v.toString();
    }

    /**
     * Converts a table into camel case.
     * @param t The table to convert.
     * @return Camel case format.
     */
    public static String tableToCamel(String t) {
        StringBuilder v = new StringBuilder();
        String s[] = t.split("\\_+");
        boolean first = true;
        for (String i:s) {
            if (first) {
                v.append(i.toLowerCase());
            } else if (i.length()==1) {
                v.append(Character.toUpperCase(i.charAt(0))).append(' ');
            } else {
                v.append(Character.toUpperCase(i.charAt(0))).append(i.substring(1).toLowerCase());
            }
            first = false;
        }
        return v.toString();
    }

    /**
     * Provides a quick test of this code.
     *
     * @param args The arguments.
     */
    public static void main (String args[]) {
        CaseConverter c = new CaseConverter();
        System.out.println(c.camelToHuman("hiThereMarco"));
        System.out.println(c.camelToHuman("hiTThereMMarco"));
        System.out.println(c.tableToHuman("hey_there_how_are"));
        System.out.println(c.tableToCamel("hey_there_how_are"));
    }    
}
